package com.jiantong.news.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.news.impl.dao.QuotesDao;
import com.jiantong.news.model.Quotes;
import com.jiantong.news.service.QuotesReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 行情报价
 * Created by mz03 on 2017/3/16.
 *zyp
 */
@Slf4j
@Service
@RpcProvider
public class QuotesReadServiceImpl implements QuotesReadService{

    private final QuotesDao quotesDao;

    public QuotesReadServiceImpl (QuotesDao quotesDao){
        this.quotesDao = quotesDao;
    }

    /*
    根据ID查询单条报价行情
     */
    @Override
    public Response<Quotes> finById(Long id) {
        try{
            Quotes quotes = quotesDao.findById(id);
            if(quotes == null){
                //判断根据id查出来的结果,如果是空值 抛异常信息
                log.error("quotes (id={}) is not found", id);
                return Response.fail("quotes.not.found");
            }
            return Response.ok(quotes);
        } catch (Exception e) {
            log.error("failed to find quotes(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("quotes.find.fail");
        }
    }

    /**
     * type 查找
     * @param type
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findByType(Integer type) {
        try{
            List<Quotes> quotesList  = quotesDao.findByType(type);
           return Response.ok(quotesList);
        }catch (Exception e){
            log.error("find by quotesList type: {}  fail, cause: {}",Throwables.getStackTraceAsString(e));
            return Response.fail("find quotesList fail");
        }
    }

    /**
     * 根据指定字段查询
     * @param state
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findByState(Integer state){
        try{
            Map<String,Object> criteria = Maps.newHashMap();
            if(state != null) {
                criteria.put("state", state);
            }
            return Response.ok(quotesDao.findByList(criteria));
        }catch (Exception e){
            log.error("failed to fin quotes(state={}),cause: {}",state,Throwables.getStackTraceAsString(e));
            return Response.fail("quotes find fail");
        }


    }

    /**
     * 根据省查找全部
     *
     * @param province
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findByProvince(String province) {
        try{
            Map<String,Object> criteria = Maps.newHashMap();
            if(province != null) {
                criteria.put("province", province);
            }
            return Response.ok(quotesDao.findByList(criteria));
        }catch (Exception e){
            log.error("failed to fin quotes(province={}),cause: {}",province,Throwables.getStackTraceAsString(e));
            return Response.fail("quotes find fail");
        }
    }

    /**
     * 根据省查找最新的一组
     * @param province
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findByNewProvince(String province) {
        try {
            return Response.ok(quotesDao.findByNewProvince(province));
        }catch (Exception e){
            log.error("find by quotes province:{} fail,cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("find.quotes.fail");
        }
    }

    /**
     * 根据类型查找所有省份最新的价格
     *
     * @param type
     * @param date
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findByNewType(Integer type, Date date) {
        try{
            Map<String,Object> criteria = Maps.newHashMap();
            if(date != null||type!=null) {
                //处理date让它是当前日期的00点00分
                SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
                String time = formatter.format(date);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                date = dateFormat.parse(time);
                //拿到加一天的data
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.DATE, 1);
                Date stopTime = cal.getTime();

                criteria.put("type",type);
                criteria.put("date", date);
                criteria.put("stopTime",stopTime);
            }
            return Response.ok(quotesDao.findByDateType(criteria));
        }catch (Exception e){
            log.error("failed to fin quotes(date={},type={}),cause: {}",date,type,Throwables.getStackTraceAsString(e));
            return Response.fail("quotes find fail");
        }
    }

    /**
     * 根据type和省份查询15天内的价格
     * @param type
     * @param province
     * @return List<Quotes>
     */
    @Override
    public Response<List<Quotes>> findBy15Day(Integer type,String province){
        try{
            Map<String,Object> criteria = Maps.newHashMap();
            if(type != null || province != null) {
                criteria.put("type",type);
                criteria.put("province",province);
            }
            return Response.ok(quotesDao.findBy15Day(criteria));
        }catch (Exception e){
            log.error("failed to fin quotes(type={},province={}),cause: {}",type,province,Throwables.getStackTraceAsString(e));
            return Response.fail("quotes find fail");
        }
    }
}
